package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Zone;
import com.nondo.dean.platform.domain.ZoneExample;
import com.nondo.dean.platform.domain.ZoneProvince;
import com.nondo.dean.platform.mapper.ZoneMapper;
import com.nondo.dean.platform.model.AreaBean;
import com.nondo.dean.platform.model.ZoneBean;
import com.nondo.dean.platform.model.param.ZoneParam;
import com.nondo.dean.platform.model.result.ZoneResult;

/**
 * 自定义区域管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class ZoneService {

    @Autowired
    private ZoneMapper zoneMapper;
    
    @Autowired
    private ZoneProvinceService zoneProvinceService;

    /**
     * 插入自定义区域
     * @param param {@link ZoneParam}
     * @return
     */
    @Transactional
    public int addZone(ZoneParam param) {
        Zone zone = ZoneParam.covertToDto(param);
        int num = zoneMapper.insertSelective(zone);// 影响行数
        addZoneProvince(param.getProvinceIds(), zone.getId());
        return num;
    }
    
    /**
     * 修改自定义区域
     * @param zone
     * @return
     */
    @Transactional
    public int updateZone(Zone zone) {
        return zoneMapper.updateByPrimaryKeySelective(zone);
    }

    /**
     * 修改自定义区域
     * @param param {@link ZoneParam}
     * @param zoneDb {@link Zone}
     * @return
     */
    @Transactional
    public int updateZone(ZoneParam param, Zone zoneDb) {
        Zone zone = ZoneParam.covertToDto(param);
        int num = zoneMapper.updateByPrimaryKey(zoneDb);
        // 删除原来的关系然后重新建立关系
        zoneProvinceService.updateZoneAllProvince(zone.getId());
        addZoneProvince(param.getProvinceIds(), zone.getId());
        return num;
    }

    /**
     * 建立区域省份关系
     * @param param {@link ZoneParam}
     * @param zoneId 区域ID
     */
    private void addZoneProvince(String provinceIds, int zoneId) {
        for (String provinceId : provinceIds.split(",")) {
            ZoneProvince zoneProvince = new ZoneProvince();
            zoneProvince.setZoneId(zoneId);
            zoneProvince.setProvinceId(Integer.parseInt(provinceId));
            zoneProvinceService.addZoneProvince(zoneProvince);
        }
    }

    /**
     * 按照主键ID查询自定义区域
     * @param id
     * @return
     */
    public Zone selectZone(Integer id) {
        return zoneMapper.selectByPrimaryKey(id);
    }

    /**
     * 自定义区域分页查询列表
     * @param offset
     * @param limit
     * @param zone
     * @return
     */
    public List<Zone> getZoneList(int offset, int limit, Zone zone) {
        ZoneExample example = new ZoneExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ZoneExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, zone);
        return zoneMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 自定义区域总数统计
     * @param zone
     * @return
     */
    public Long count(Zone zone) {
        ZoneExample example = new ZoneExample();
        ZoneExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, zone);
        return zoneMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param zone
     */
    void setCriteria(ZoneExample.Criteria criteria, Zone zone) {
        if (zone != null) {
            if (zone.getId() != null)
                criteria.andIdEqualTo(zone.getId());
            if (StringUtils.isNoneBlank(zone.getName()))
                criteria.andNameLike("%" + zone.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照自定义区域名称查询自定义区域
    * @param name
    * @return
    */
    public Zone selectZoneByName(String name) {
        ZoneExample example = new ZoneExample();
        ZoneExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Zone> zoneList = zoneMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(zoneList)?zoneList.get(0):null;
    }
    
    /**
     * 自定义区域列表
     * @return 不分页的区域列表List<{@link Zone}>
     */
    public List<ZoneBean> getZoneList() {
        List<ZoneBean> resultList = new ArrayList<ZoneBean>();
        ZoneExample example = new ZoneExample();
        ZoneExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Zone> zoneList = zoneMapper.selectByExample(example);
        for (Zone zone : zoneList) {
            ZoneBean zoneBean = ZoneBean.covertToBean(zone);
            resultList.add(zoneBean);
        }
        return resultList;
    }
    
    /**
     * 自定义区域列表
     * @return 包含省份的区域列表 List<{@link ZoneResult}>
     */
    public List<ZoneResult> getZoneProvinceList() {
        List<ZoneResult> resultList = new ArrayList<ZoneResult>();
        ZoneExample example = new ZoneExample();
        example.setOrderByClause("create_date DESC");
        ZoneExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Zone> zoneList = zoneMapper.selectByExample(example);
        for (Zone zone : zoneList) {
            ZoneResult zoneResult = new ZoneResult();
            ZoneBean zoneBean = ZoneBean.covertToBean(zone);
            zoneResult.setZone(zoneBean);
            List<AreaBean> provinceList = new ArrayList<AreaBean>();
            provinceList = zoneProvinceService.getProvinceListByZoneId(zone.getId());
            zoneResult.setProvinceList(provinceList);
            resultList.add(zoneResult);
        }
        return resultList;
    }
   
}
